/**
 * 页面管理
 */
var editor;
var setting = {
		view: {
			dblClickExpand: false,
			selectedMulti: false
		},
		async: {
			enable: true,
			url:"/index.php/sysadmin/articletype/search",				
			dataFilter: filter
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			beforeClick: beforeClick,
			onClick: onClick
		}
	};



	function beforeClick(treeId, treeNode) {
//		var check = (treeNode && !treeNode.isParent);
//		if (!check) alert("只能选择城市...");
//		return check;
	}
	
	function onClick(e, treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
		nodes = zTree.getSelectedNodes(),
		pid,
		v = "";
		v = nodes[0].name;
		pid = nodes[0].id;
		$('#pidSel').attr('value', v);
		$('#typeid').attr('value', pid);
		hideMenu();

	}

	function showMenu() {
		var cityObj = $("#pidSel");
		var cityOffset = $("#pidSel").offset();
		$("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");

		$("body").bind("mousedown", onBodyDown);
	}
	function hideMenu() {
		$("#menuContent").fadeOut("fast");
		$("body").unbind("mousedown", onBodyDown);
	}
	function onBodyDown(event) {
		if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
			hideMenu();
		}
	}
	function filter(treeId, parentNode, childNodes) {
		if (!childNodes) return null;
		for (var i=0, l=childNodes.length; i<l; i++) {
			childNodes[i].name = childNodes[i].title;
			childNodes[i].pId = childNodes[i].parentid;
			childNodes[i].open = true;
//			childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
		}
		return childNodes;
	}
$(function(){
	//树状图
	$.fn.zTree.init($("#treeDemo"), setting);

	$('#batch_do').click(function(){
		var ac = $('#batch_select').val();
		var msg = (ac == 'delete')?'删除':' 不知干嘛';
		var sel = [];
		
		$('input[name="chk"]').each(function(i){
			if($(this).attr('checked')){
				sel.push($(this).val());
			}
		});
		if(sel.length>0){
			if(ac=='delete'){
				$h.confirm('真的要'+msg, function(m){
					if(m=='ok'){
						$h.load('/sysadmin/article/'+ac+'/'+sel.join('-'),'', function(d){
							for(var i=0; i<sel.length; i++){
								$('#tr_'+sel[i]).remove();
							}
						});
					}
				});
			}
			
		}else{
			$h.alert('木有选择啊，亲');
		}
		return ;
	});
	
	$('a[href="#tab2"]').click(function(){		
		$('#frmadd input').each(function(i){
			if($(this).attr('type')=='text' || $(this).attr('type')=='hidden'){
				$(this).val('');
			}
		});
		if(!editor){
			editor = KindEditor.create('textarea[name="content"]');			
		}else{
			editor.html('');
		}
		$('#sbm').val('添加');
	});
	$('#sbm').click(function(){
		if($('#title').val()!=''){
			$('#content').val(editor.html());
			var p = $('#frmadd').serialize();
			$h.load('/sysadmin/article/add', p, function(d){
				if(d && d>0){
					location.href=location;
				}else{
					$h.alert('添加失败');return;
				}
			});
		}else{
			$h.alert('烦请填上标题');
			return false;
		}
		return;
	});
	
});
$h.include([
            '/resources/scripts/kindeditor/kindeditor-min.js',
            '/resources/scripts/kindeditor/lang/zh_CN.js',
            '/resources/scripts/ztree/js/jquery.ztree.core-3.1.js',
            '/resources/scripts/ztree/css/zTreeStyle/zTreeStyle.css'
            ]);

function del(id){
	$h.confirm('确认删除', function(msg){		
		if(msg == 'ok'){
			$h.load('/sysadmin/article/delete/'+id,'', function(d){
				$('#tr_'+id).remove();
				
			});
			return true;
		}else{
			return false;
		}		
		
	});
	
}
function edit(id){
	$('a[href="#tab2"]').click();
	$h.load('/sysadmin/article/add/'+id, '', function(d){
		log.debug('---请求的数据--');
		log.debug(d);
		if(d && d.length>0){
			var p=d[0];
			$h.fillFormInput_p('frmadd', p);
			editor.insertHtml(p.content);
			
			$('#sbm').val('编辑');
		}
	})
}